﻿//反射的局限性

//比较普通调用和反射调用的耗时
using Business.DB.Interface;
using Business.DB.Sqlserver;
using System.Diagnostics;
using System.Reflection;
using 反射;

Stopwatch stopwatch = Stopwatch.StartNew();
for (var i = 0; i < 1000000; i++)
{
    IDBHelper dbHelper = new SqlserverHelper();
    dbHelper.Query();
}
stopwatch.Stop();

Stopwatch stopwatch2 = Stopwatch.StartNew();
Assembly assembly1 = Assembly.LoadFrom("Business.DB.Sqlserver.dll");
Type type = assembly1.GetType("Business.DB.Sqlserver.SqlserverHelper");
for (var i = 0; i < 1000000; i++)
{
    var oInstance = Activator.CreateInstance(type) as IDBHelper;
    oInstance.Query();
}
stopwatch2.Stop();

Console.WriteLine($"普通调用耗时{stopwatch.ElapsedMilliseconds}");//
Console.WriteLine($"反射调用耗时{stopwatch2.ElapsedMilliseconds}");//

//反射存在明显性能损耗
//但是，在反射代码合理情况下，性能损失很小，仅仅是毫秒级